package org.webArchi.common.api.domain;

/**
 *
 * Bean class for user
 */


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;


@Entity
@Table(name = "USERS", schema = "COMMON")
@NamedQueries({
        // @NamedQuery(name = "filter.by.roleid", query = "SELECT u FROM User u " +
        //    "WHERE u.status != 3 AND u.role.roleId = :roleId " +
        //   "ORDER BY u.userId")
})
public class User implements Serializable {

    public enum State {
        INACTIVE, // 0 - Cannot login
        ACTIVE,// 1 -  Can login
        LOCKEDOUT,//2 - state. Cannot login
        DELETED //3 - state. Permanently deleted cannot be edited or login
    }

    private static final Logger logger = LoggerFactory.getLogger(User.class);

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(updatable = false, length = 30, nullable = false)
    private String userId;

    // The SHA-1 hash of the password
    @Column(length = 60, nullable = false)
    private String passwordHash;

    // The preferred language
    @Column(length = 2, nullable = true)
    private String prefLanguage;

    //The password expiry date, after which the user is not allowed to login without changing the password
    @Column(nullable = true)
    private Date passwordExpiry;


    @Column(nullable = false, columnDefinition = "smallint not null default 1")
    private State status;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    @JoinColumn(name = "roleId")
    private Role role;

    //Person informations
    @Column(nullable = false)
    private String nic;

    @Column(nullable = true)
    private String firstName;

    @Column(nullable = true)
    private String lastName;

    @Column(nullable = true)
    private Date dateOfBirth;


    @Column(nullable = true)
    private String street;

    @Column(nullable = true)
    private String city;

    @Column(nullable = true)
    private String postalCode;

    @Column(nullable = true)
    private String emailAddress;

    //TP informations
    @OneToMany
    @JoinTable(name = "TPNUMBERS", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "tpNumber"))
    private Collection<TPNumber> tpNumbers;

    //Basic life cycle information for this object
    @Embedded
    private BaseLifeCycleInfo lifeCycleInfo = new BaseLifeCycleInfo();

    //Default consturctor
    public User() {
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        User user = (User) o;
        if (userId != null ? !userId.equals(user.userId) : user.userId != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return userId != null ? userId.hashCode() : 0;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getPrefLanguage() {
        return prefLanguage;
    }

    public void setPrefLanguage(String prefLanguage) {
        this.prefLanguage = prefLanguage;
    }

    public Date getPasswordExpiry() {
        return passwordExpiry;
    }

    public void setPasswordExpiry(Date passwordExpiry) {
        this.passwordExpiry = passwordExpiry;
    }

    public State getStatus() {
        return status;
    }

    public void setStatus(State status) {
        this.status = status;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public BaseLifeCycleInfo getLifeCycleInfo() {
        return lifeCycleInfo;
    }

    public void setLifeCycleInfo(BaseLifeCycleInfo lifeCycleInfo) {
        this.lifeCycleInfo = lifeCycleInfo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNic() {
        return nic;
    }

    public void setNic(String nic) {
        this.nic = nic;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Collection<TPNumber> getTpNumbers() {
        return tpNumbers;
    }

    public void setTpNumbers(Collection<TPNumber> tpNumbers) {
        this.tpNumbers = tpNumbers;
    }

}